package org.jeecg.modules.approve.cancel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.approve.cancel.entity.BikeApproveCancel;
import org.jeecg.modules.approve.cancel.mapper.BikeApproveCancelMapper;
import org.jeecg.modules.approve.cancel.service.IBikeApproveCancelService;
import org.jeecg.modules.approve.cancel.vo.BikeApproveCancelRes;
import org.jeecg.modules.bicycle.order.entity.BikeVehicleOrder;
import org.jeecg.modules.bicycle.order.mapper.BikeVehicleOrderMapper;
import org.jeecg.modules.constant.BikeConstant;
import org.jeecg.modules.param.charge.mapper.BikeMemberUserChargeMapper;
import org.jeecg.modules.param.member.entity.BikeMemberUser;
import org.jeecg.modules.param.member.entity.BikeMemberUserCard;
import org.jeecg.modules.param.member.mapper.BikeMemberUserCardMapper;
import org.jeecg.modules.param.member.mapper.BikeMemberUserMapper;
import org.jeecg.modules.param.refund.entity.BikeMemberUserReturn;
import org.jeecg.modules.utils.page.PageInfo;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Description: 销户审批
 * @Author: 月月鸟
 * @Date:   2021-09-02
 * @Version: V1.0
 */
@Service
public class BikeApproveCancelServiceImpl extends ServiceImpl<BikeApproveCancelMapper, BikeApproveCancel> implements IBikeApproveCancelService {
    @Resource
    private BikeApproveCancelMapper bikeApproveCancelMapper;
    @Resource
    private BikeMemberUserMapper bikeMemberUserMapper;
    @Resource
    private BikeMemberUserCardMapper  bikeMemberUserCardMapper;
    @Resource
    private BikeVehicleOrderMapper bikeVehicleOrderMapper;
    @Override
    public Result<?> pageList(BikeApproveCancelRes bikeApproveCancelRes, Integer pageNo, Integer pageSize) {
        Page<BikeApproveCancelRes> page = new Page<>(pageNo, pageSize);
        Page<BikeApproveCancelRes> pageList = bikeApproveCancelMapper.pageList(page, bikeApproveCancelRes);
        PageInfo pageInfo = new PageInfo();
        pageInfo.setTotal(pageList.getTotal());
        pageInfo.setRecords(pageList.getRecords());
        pageInfo.setCurrent(pageList.getCurrent());
        pageInfo.setSize(pageList.getSize());
        return Result.OK(pageInfo);
    }

    @Override
    public Result<?> approve(BikeApproveCancel bikeApproveCancel) {
        BikeApproveCancel selectById = bikeApproveCancelMapper.selectById(bikeApproveCancel);
        //判断审批信息是否已经被审批
        if (!selectById.getApproveStatus().equals("0")){
            return Result.error("已审批，无法再次审批");
        }
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //审批人名称
        bikeApproveCancel.setHandleBy(sysUser.getRealname());
        //审批时间
        bikeApproveCancel.setHandleTime(new Date());
        if (StringUtils.isNotEmpty(bikeApproveCancel.getApproveStatus())){
            if (bikeApproveCancel.getApproveStatus().equals("1")){
                //TODO: 2021/9/2 审批成功后 执行销户
                String yn = refundUser(bikeApproveCancel);
                //不成功传问题
                if (!yn.equals(BikeConstant.y_n_yes)){
                    return Result.error(yn);
                }
            }
        }
        bikeApproveCancelMapper.updateById(bikeApproveCancel);
        return Result.OK("审批成功");
    }
    //审核通过后的销户流程
    private String refundUser(BikeApproveCancel bikeApproveCancel) {
        //获取用户信息
        BikeMemberUser memberUserData = bikeMemberUserMapper.selectById(bikeApproveCancel.getUserId());
        //修改状态对象
        memberUserData.setStatus(BikeConstant.member_status_logout);
        memberUserData.setIsCertification(BikeConstant.y_n_no);
        memberUserData.setIdCard("");
        memberUserData.setBalance(new BigDecimal("0"));
        memberUserData.setStatus("4");
        bikeMemberUserMapper.updateById(memberUserData);
        return BikeConstant.y_n_yes;

    }
    @Override
    public List<BikeApproveCancelRes> exportList(BikeApproveCancelRes bikeApproveCancelRes) {
        return bikeApproveCancelMapper.list(bikeApproveCancelRes);
    }

    //校验销户
    @Override
    public String addCheck(BikeApproveCancel bikeApproveCancel) {
        BikeMemberUser memberData = bikeMemberUserMapper.selectById(bikeApproveCancel.getUserId());
        QueryWrapper<BikeApproveCancel> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",bikeApproveCancel.getUserId());
        wrapper.eq("approve_status","0");
        BikeApproveCancel bikeApproveCancelData = bikeApproveCancelMapper.selectOne(wrapper);
        //判断是否重复申请销户
        if (bikeApproveCancelData!=null){
            if (bikeApproveCancelData.getApproveStatus().equals("0")){
                return "销户审批中，请勿重复提交";
            }
        }

        ////判断是否有卡
        QueryWrapper<BikeMemberUserCard> ucw = new QueryWrapper<>();
        ucw.eq("user_id",memberData.getId());
        BikeMemberUserCard userCard = bikeMemberUserCardMapper.selectOne(ucw);
        Integer selectCount = bikeVehicleOrderMapper.selectCount(new QueryWrapper<BikeVehicleOrder>()
                .ne("ride_status", "5")
                .eq("pick_user", bikeApproveCancel.getUserId())
        );
        if (selectCount!=0){
            return "有未完结订单，请核实后销户！";
        }
        //判断状态是否正常
        if (!memberData.getStatus().equals("1")){
            return "正常状态才能销户";
        }
        if (userCard!=null){
            return "请先退卡后销户";
        }
        return BikeConstant.y_n_yes;
    }
}
